package com.smsc.headend.task.engine.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.smsc.headend.module.asset.dmo.MeterConfigDMO;
import com.smsc.headend.module.asset.dto.MeterDTO;
import com.smsc.headend.module.asset.entity.ComEntity;
import com.smsc.headend.module.asset.entity.Meter;
import com.smsc.headend.module.asset.entity.PlcMeterStatus;
import com.smsc.headend.task.engine.service.DeviceStatusService;
import com.smsc.headend.task.engine.service.FeignAssetManagementService;
import com.smsc.headend.task.engine.service.asset.AssetService;
import lombok.extern.slf4j.Slf4j;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Slf4j
@Service

public class DeviceStatusServiceImpl implements DeviceStatusService {
    @Autowired
    FeignAssetManagementService assetManagementService;

    @Autowired
    AssetService assetService;

    @Override
    public void updateMeterReadDataStatus(Meter meter) {
        try {
            if (meter != null) {
                MeterDTO meterDTO = new MeterDTO();
                meterDTO.setMeterId(meter.getMeterId());
                boolean isNeedUpdate = updateInstallStatus(meterDTO, meter) | updateRegisterStatus(meterDTO, meter) | updateMountStatus(meterDTO, meter);
                if (isNeedUpdate) {
                    assetManagementService.updateMeterStatus(meterDTO);
                }
                if (meter.getChannelType().equals("PLC")) {
                    // 更新PLC表的在线状态
                    updatePlcOnlineStatus(meter);
                }
                updateComEntityStatus(meter);
            }
        } catch (Exception e) {
            log.info("updateDeviceStatus error {}", e.getMessage());
        }
    }

    private boolean updateInstallStatus(MeterDTO meterDTO, Meter meter) {
        if (meter.getInstallStatus() != null && meter.getInstallStatus() != Meter.MeterInstallStatus.Installed.getCode()) {
            // 更新安装状态
            meterDTO.setInstallStatus(Meter.MeterInstallStatus.Installed.getCode());
            log.debug("update meter install status {}", meter.getMeterId());
            return true;
        }
        return false;
    }

    private boolean updateRegisterStatus(MeterDTO meterDTO, Meter meter) {
        if (meter.getRegisterStatus() != null && meter.getRegisterStatus() != Meter.MeterRegisterStatus.Registerd.getCode()) {
            // 更新注册状态
            meterDTO.setRegisterStatus(Meter.MeterRegisterStatus.Registerd.getCode());
            log.debug("update meter register status {}", meter.getMeterId());
            return true;
        }
        return false;
    }

    private boolean updateMountStatus(MeterDTO meterDTO, Meter meter) {
        if (meter.getChannelType().equals("PLC") && meter.getMountStatus() != null && meter.getMountStatus() != Meter.MeterMountStatus.Mounted.getCode()) {
            // 更新注挂接状态
            meterDTO.setMountStatus(Meter.MeterMountStatus.Mounted.getCode());
            log.debug("update meter mount status {}", meter.getMeterId());
            return true;
        }
        return false;
    }

    private void updateComEntityStatus(Meter meter) {
        try {
            MeterConfigDMO meterConfigById = assetManagementService.getMeterConfigById(meter.getMeterId());
            if (meterConfigById == null) {
                return;
            }
            Long comId = meterConfigById.getComId();
            ComEntity comEntity = assetService.getComById(comId);
            // 不是offline状态间隔默认时间刷新tv,如果是offLine状态，根据comStatus刷新为在线
            if (isComEntityOffline(comEntity)) {
                assetManagementService.updateComEntityStatus(comEntity.getComId(), ComEntity.CommunicationStatus.ONLINE.getCode());
            }
        } catch (Exception e) {
            log.info("updateComEntityStatus error {}", e.getMessage());
        }
    }

    private boolean isComEntityOffline(ComEntity comEntity) {
        return (ObjectUtil.equals(ComEntity.CommunicationStatus.OFFLINE.getCode(), comEntity.getComStatus()) || ObjectUtil.equals(ComEntity.CommunicationStatus.NEVER.getCode(), comEntity.getComStatus()));
    }

    private void updatePlcOnlineStatus(Meter meter) {
        try {
            PlcMeterStatus plcMeterStatus = assetManagementService.getPlcMeterStatus(meter.getMeterId());
            if (plcMeterStatus == null) {
                plcMeterStatus = new PlcMeterStatus();
                plcMeterStatus.setStatus(PlcMeterStatus.Status.Offline.getCode());
                plcMeterStatus.setMeterId(meter.getMeterId());
            }
            if (plcMeterStatus.getStatus() != PlcMeterStatus.Status.Online.getCode()) {
                assetManagementService.updatePlcMeterStatus(meter.getMeterId(), PlcMeterStatus.Status.Online.getCode(), null);
            }
        } catch (Exception e) {
            log.info("updatePlcOnlineStatus error {}", e.getMessage());
        }

    }
}
